package com.ms.p2p.controller.admin;


import com.alibaba.dubbo.config.annotation.Reference;
import com.ms.common.bean.Message;
import com.ms.common.utils.MsDateUtils;
import com.ms.common.utils.excel.ExcelData;
import com.ms.p2p.bean.PageBean;
import com.ms.p2p.controller.BaseController;
import com.ms.p2p.entity.member.Member;
import com.ms.p2p.entity.system.AdminUser;
import com.ms.p2p.plugin.validation.annotation.PermissionValidator;
import com.ms.p2p.plugin.validation.annotation.Validations;
import com.ms.p2p.provider.AdminUserProvider;
import com.ms.p2p.provider.CertiFiCationProvider;
import com.ms.p2p.provider.MailAuthProvider;
import com.ms.p2p.provider.MemberProvider;
import com.ms.p2p.utils.JsonDataGridHelper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * 用户管理
 */
@Controller("AdminMemberController")
@RequestMapping({ "/admin/member" })
public class AdminMemberController extends AdminBaseController {
	@Reference
	private AdminUserProvider emplyeeService;
	@Reference
	private MemberProvider memberProvider;
	@Reference
	private CertiFiCationProvider certificationProvider;
	@Reference
	private MailAuthProvider mailAuthProvider;


//	@RequestMapping({ "/userInfoDetail" })
//	public String userInfoDetail(HttpServletRequest request) {
//		String userId = request.getParameter("id");
//		if (userId != null && !("".equals(userId))) {
//			Member user = memberProvider.findById(Integer.parseInt(userId));
//			request.setAttribute("user", user);
//			request.setAttribute("userInfo", null);
//			Auth realName = certificationProvider.findByUserId(user.getId());
//			request.setAttribute("realName", realName);
//			request.setAttribute("pany", null);
//			Email email = mailAuthProvider.findByUserId(user.getId());
//			request.setAttribute("email", email);
//			request.setAttribute("moblie", user.getMember_phone());
//		}
//		return "admin/member/customer/register/infodetail";
//	}

//	/**
//	 * @Title: seeUserInfo
//	 * @Description: 查询用户信息
//	 * @param request
//	 * @return String 返回类型
//	 */
//	@RequestMapping({ "/seeUserInfo" })
//	public String seeUserInfo(HttpServletRequest request) {
//		String userId = request.getParameter("id");
//		if (userId != null && !("".equals(userId))) {
//			Member user = memberProvider.findById(Integer.parseInt(userId));
//			request.setAttribute("user", user);
//			Auth realName = certificationProvider.findByUserId(user.getId());
//			Email email = mailAuthProvider.findByUserId(user.getId());
//			request.setAttribute("user", user);
//			request.setAttribute("userInfo", null);
//			request.setAttribute("assetsInfo", null);
//			request.setAttribute("privateOwnerInfo", null);
//			request.setAttribute("financeCondition", null);
//			request.setAttribute("contactWay", null);
//			request.setAttribute("spouseInfo", null);
//			request.setAttribute("borrower", null);
//			request.setAttribute("realName", realName);
//			request.setAttribute("email", email);
//			request.setAttribute("moblie", user.getMember_phone());
//
//		}
//		return "admin/member/customer/register/info";
//	}


	@RequestMapping(value = {"/queryData"}, produces = {"application/json;charset=UTF-8"})
	@ResponseBody
	public PageBean queryData(HttpServletRequest request, HttpSession session
			, @RequestParam(value = "userId",required = false) String userId
			, @RequestParam(value = "userName",required = false) String userName
			, @RequestParam(value = "mobilePhone",required = false) String mobilePhone
			, @RequestParam(value = "begin_date",required = false) String begin_date
			, @RequestParam(value = "email",required = false) String email
			, @RequestParam(value = "end_date",required = false) String end_date
			, @RequestParam(value = "ru_userId",required = false) String ru_userId
			, @RequestParam(value = "ru_userName",required = false) String ru_userName
			, @RequestParam(value = "kefuname",required = false) String kefuname
			, @RequestParam(value = "ru_mobilePhone",required = false) String ru_mobilePhone
			, PageBean page
	) {

		page.setList(memberProvider.findPager(
				userId,userName,kefuname,email,mobilePhone,begin_date,
				end_date,ru_userId,ru_userName,ru_mobilePhone,page));
		page.setTotalCount(memberProvider.findCount(userId,userName,kefuname,email,mobilePhone,
				begin_date,end_date,ru_userId,ru_userName,ru_mobilePhone));
		return JsonDataGridHelper.createJSONData(page);
	}

	/**
	 * 后台客户注册详情
	 */
	@RequestMapping({ "/list" })
	public String list(HttpServletRequest request


	) {

		return "admin/member/customer/register/list";
	}


	/**
	 * 导出后台客户注册详情
	 *
	 * @param session
	 * @return
	 */
	@RequestMapping({ "/exportCustomerList" })
	public String exportCustomerList(HttpSession session
			, @RequestParam(value = "userId",required = false) String userId
			, @RequestParam(value = "userName",required = false) String userName
			, @RequestParam(value = "mobilePhone",required = false) String mobilePhone
			, @RequestParam(value = "begin_date",required = false) String begin_date
			, @RequestParam(value = "email",required = false) String email
			, @RequestParam(value = "end_date",required = false) String end_date
			, @RequestParam(value = "ru_userId",required = false) String ru_userId
			, @RequestParam(value = "ru_userName",required = false) String ru_userName
			, @RequestParam(value = "kefuname",required = false) String kefuname
			, @RequestParam(value = "ru_mobilePhone",required = false) String ru_mobilePhone
			,Model model
	){

		AdminUser emp = (AdminUser) session.getAttribute("adminUser");
		if (emp != null && ("1".equals(emp.getDepart()+"") || "33".equals(emp.getDepart()+""))) {

			List<Map<String, Object>> users  = memberProvider.findAll(userId, userName, kefuname,email, mobilePhone, begin_date, end_date, ru_userId, ru_userName, ru_mobilePhone);
			for(Map<String, Object> map : users){
				String addtime = (String) map.get("add_time");
				if (addtime != null) {
					map.put("add_time", MsDateUtils.TimeStamp2DateTime(addtime));
				}
			}
			ExcelData excelData = new ExcelData();
			excelData.setHeads(
					new String[]{"用户ID", "用户名称", "真实姓名","企业名称", "手机号", "邮箱", "推荐人ID", "推荐人用户名", "推荐人手机号", "注册时间", "所属客服"});
			excelData.setFieldNames(new String[]{"id", "user_name","realName","pany_name", "member_phone", "email",
					"recommend_id", "ru_user_name", "ru_mobile", "add_time", "kefuname"});
			excelData.setDataList(users);
			excelData.setModelClass(Map.class);
			model.addAttribute("excelData", excelData);
			model.addAttribute("fileName", "注册详情");
		}
		return "excel";
	}

	/**
	 * 到关联管理员
	 *
	 * @param request
	 * @return
	 */
	@Validations(perms = { @PermissionValidator(permission = "user::customers::correlation") })
	@RequestMapping("/service")
	public String toAssociateEmployee(HttpServletRequest request,
									  @RequestParam("id") int id) {
		Member user = memberProvider.findById(id);
		List<AdminUser> list = emplyeeService.findByDepart(31);
		request.setAttribute("user", user);
		request.setAttribute("kefulist", list);
		return "admin/member/customer/register/service";
	}

	/**
	 * 关联管理员
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/saveService", produces = "application/json;charset=UTF-8")
	@ResponseBody
	@Validations(perms = { @PermissionValidator(permission = "user::customers::correlation") })
	public Message associateEmployee(HttpServletRequest request, RedirectAttributes redirectAttributes,
									@RequestParam("id") Integer id,
									@RequestParam("kefu_id") Integer kefu_id) {
		memberProvider.updateKefuByid(id, kefu_id + "");
//		addFlashMessage(redirectAttributes, Message.success("关联成功"));

		return Message.success("关联成功");
	}

	@Override
	public boolean getBinderFlag() {
		return true;
	}
}
